package summer.presum;

public class Solution724 {
    public int pivotIndex(int[] nums) {
        int preSum = 0;
        int n = nums.length;
        int[] sum = new int[n];
        for (int i = 0; i < n; i++) {
            sum[i] = preSum + nums[i];
            preSum = sum[i];
        }
        if (sum[n - 1] - sum[0] == 0) return 0;
        for (int i = 1; i < n - 1; i++) {
            if (sum[i - 1] == sum[n - 1] - sum[i]) return i;
        }
        if (sum[n - 1] - nums[n - 1] == 0) return n - 1;
        return -1;
    }
}
